package com.ruoyi.factorydata.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.factorydata.domain.ModelInventory;
import com.ruoyi.factorydata.domain.dto.ModelInventoryPrintDTO;
import com.ruoyi.factorydata.service.IModelInventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 母版库存Controller
 *
 * @author ruoyi
 * @date 2025-07-27
 */
@RestController
@RequestMapping("/warehouse/modelInventory")
public class ModelInventoryController extends BaseController {
    @Autowired
    private IModelInventoryService modelInventoryService;

    /**
     * 查询母版库存列表
     */
    @PreAuthorize("@ss.hasPermi('warehouse:modelInventory:list')")
    @GetMapping("/list")
    public TableDataInfo list(ModelInventory modelInventory) {
        startPage();
        List<ModelInventory> list = modelInventoryService.selectModelInventoryList(modelInventory);
        return getDataTable(list);
    }
    /**
     * 查询母版打印信息
     */
    @PreAuthorize("@ss.hasPermi('warehouse:modelInventory:print')")
    @GetMapping("/print/list")
    public ModelInventoryPrintDTO printList(ModelInventory modelInventory) {
        return modelInventoryService.printList(modelInventory);
    }

    /**
     * 导出母版库存列表
     */
    @PreAuthorize("@ss.hasPermi('warehouse:modelInventory:export')")
    @Log(title = "母版库存", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ModelInventory modelInventory) {
        List<ModelInventory> list = modelInventoryService.selectModelInventoryList(modelInventory);
        ExcelUtil<ModelInventory> util = new ExcelUtil<ModelInventory>(ModelInventory.class);
        util.exportExcel(response, list, "母版库存数据");
    }

    /**
     * 获取母版库存详细信息
     */
    @PreAuthorize("@ss.hasPermi('warehouse:modelInventory:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(modelInventoryService.selectModelInventoryById(id));
    }
    /**
     * 获取母版库存详细信息
     */
    @GetMapping(value = "/scanner/{id}")
    public AjaxResult getScannerInfo(@PathVariable("id") Long id) {
        return success(modelInventoryService.getScannerInfo(id));
    }
    /**
     * 新增母版库存
     */
    @PreAuthorize("@ss.hasPermi('warehouse:modelInventory:add')")
    @Log(title = "母版库存", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ModelInventory modelInventory) {
        return toAjax(modelInventoryService.insertModelInventory(modelInventory));
    }

    /**
     * 修改母版库存
     */
    @PreAuthorize("@ss.hasPermi('warehouse:modelInventory:edit')")
    @Log(title = "母版库存", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ModelInventory modelInventory) {
        return toAjax(modelInventoryService.updateModelInventory(modelInventory));
    }

    /**
     * 母版报废
     */
    @PreAuthorize("@ss.hasPermi('warehouse:modelInventory:remove')")
    @Log(title = "母版库存", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(modelInventoryService.deleteModelInventoryByIds(ids));
    }

    /**
     * 查询物料库存信息列表
     */
    @PreAuthorize("@ss.hasPermi('warehouse:modelInventory:list')")
    @GetMapping("/list/count")
    public AjaxResult listInventoryCount(ModelInventory modelInventory) {
        Long listInventoryCount = modelInventoryService.countModelInventoryList(modelInventory);
        return AjaxResult.success(listInventoryCount);
    }


    @GetMapping("/modelCode/list")
    public AjaxResult listModelCode(ModelInventory modelInventory) {
        List<ModelInventory> list = modelInventoryService.selectModelCodeList(modelInventory);
        return AjaxResult.success(list);
    }

    /**
     * 母版出库
     */
    @PreAuthorize("@ss.hasPermi('warehouse:modelInventory:remove')")
    @Log(title = "母版库存", businessType = BusinessType.UPDATE)
    @PutMapping("/out")
    public AjaxResult out(@RequestBody ModelInventory modelInventory) {
        return toAjax(modelInventoryService.outModelInventory(modelInventory));
    }


    /**
     * 母版归还出库
     */
    @PreAuthorize("@ss.hasPermi('warehouse:modelInventory:add')")
    @Log(title = "母版库存", businessType = BusinessType.UPDATE)
    @PutMapping("/revert")
    public AjaxResult revert(@RequestBody ModelInventory modelInventory) {
        return toAjax(modelInventoryService.revertInModelInventory(modelInventory));
    }
}
